package com.atguigu.lease.web.admin.controller.apartment;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.RoomInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryDto;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitDyo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
public class RoomController {
  @Autowired private RoomInfoService roomInfoService;

  @Operation(summary = "保存或更新房间信息")
  @PostMapping("saveOrUpdate")
  public Result saveOrUpdate(@RequestBody RoomSubmitDyo roomSubmitDyo) {
    return Result.build(roomInfoService.saveOrUpdateRoom(roomSubmitDyo));
  }

  @Operation(summary = "根据条件分页查询房间列表")
  @GetMapping("pageItem")
  public Result<IPage<RoomItemVo>> pageItem(
      @RequestParam long current, @RequestParam long size, RoomQueryDto queryVo) {
    IPage<RoomItemVo> page = new Page<>(current, size);
    IPage<RoomItemVo> list = roomInfoService.pageItem(page, queryVo);
    return Result.ok(list);
  }

  @Operation(summary = "根据id获取房间详细信息")
  @GetMapping("getDetailById")
  public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
    RoomDetailVo roomDetailVo = roomInfoService.getRoomDetailById(id);
    return Result.ok(roomDetailVo);
  }

  @Operation(summary = "根据id删除房间信息")
  @DeleteMapping("removeById")
  public Result removeById(@RequestParam Long id) {
    return Result.build(roomInfoService.removeRoomById(id));
  }

  @Operation(summary = "根据id修改房间发布状态")
  @PostMapping("updateReleaseStatusById")
  public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
    roomInfoService.update(
        Wrappers.lambdaUpdate(RoomInfo.class)
            .eq(RoomInfo::getId, id)
            .set(RoomInfo::getIsRelease, status));
    return Result.ok();
  }

  @GetMapping("listBasicByApartmentId")
  @Operation(summary = "根据公寓id查询房间列表")
  public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
    // LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
    // queryWrapper.eq(RoomInfo::getApartmentId, id);
    // queryWrapper.eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED);
    List<RoomInfo> roomInfoList =
        roomInfoService.list(
            Wrappers.lambdaQuery(RoomInfo.class)
                .eq(RoomInfo::getApartmentId, id)
                .eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED));
    return Result.ok(roomInfoList);
  }
}
